// JeolDlg.cpp : implementation file

#include "stdafx.h"
#include "JeolApp.h"
#include "JeolDlg.h"
//#include "Preview1.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg(void);

	enum { IDD = IDD_ABOUTBOX };

protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support

protected:
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg(void) : CDialog(CAboutDlg::IDD) {}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
END_MESSAGE_MAP()






// CJeolDlg dialog

CJeolDlg::CJeolDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CJeolDlg::IDD, pParent),
	  m_queue(30), m_resampler(theConfig.GetResampleRate()), 
	  m_nTimerID(0), m_fsm(*this)
{
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CJeolDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_PREVIEW, m_wndPreview);
}

BEGIN_MESSAGE_MAP(CJeolDlg, CDialog)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	//}}AFX_MSG_MAP
	ON_WM_CLOSE()
	ON_BN_CLICKED(IDC_BUTTON_SAVE, &CJeolDlg::OnBnClickedButtonSave)
	ON_WM_TIMER()
	ON_BN_CLICKED(IDC_LOG_BUTTON, &CJeolDlg::OnBnClickedLogButton)
END_MESSAGE_MAP()


// CJeolDlg message handlers

BOOL CJeolDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		BOOL bNameValid;
		CString strAboutMenu;
		bNameValid = strAboutMenu.LoadString(IDS_ABOUTBOX);
		ASSERT(bNameValid);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon

	const CString & strInputDump = theConfig.GetInputDump();

	if ( strInputDump.IsEmpty() )
	{
		m_pSource.Attach(new CAdcDevice);
		m_pSource->Open(0);
	}
	else
	{
		m_pSource.Attach(new CAdcDump);
		m_pSource->Open(strInputDump.GetString());
	}

	m_timeFrame = CTime::GetCurrentTime();

	m_nTimerID = SetTimer(1, 40, NULL);
	m_pSource->Start(this);

	return TRUE;
}

void CJeolDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CJeolDlg::OnPaint()
{
	if ( IsIconic() )
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

HCURSOR CJeolDlg::OnQueryDragIcon()
{
	return static_cast<HCURSOR>(m_hIcon);
}


void CJeolDlg::OnOK() {}

void CJeolDlg::OnCancel() {}

void CJeolDlg::OnClose()
{
	KillTimer(m_nTimerID);
	m_nTimerID = 0;

 	m_pSource->Stop();
 	m_pSource->Close();

	PostQuitMessage(0);
	CDialog::OnClose();
}

void CJeolDlg::ReadData(SHORT * pData, SIZE_T nLen)
{
	m_queue.Map(pData, nLen);

	do 
	{
		m_fsm.Run(m_queue);
	}
	while (m_queue.Next());
}

void CJeolDlg::OnFrameStart(void)
{
	m_wndPreview.OnFrameStart();
	CButton * pSaveButton = (CButton *) (GetDlgItem(IDC_BUTTON_SAVE));
	pSaveButton->EnableWindow(FALSE);
	m_timeFrame = CTime::GetCurrentTime();
}

void CJeolDlg::OnFrameStop(void)
{
	m_wndPreview.OnFrameStop();
	CButton * pSaveButton = (CButton *) (GetDlgItem(IDC_BUTTON_SAVE));
	pSaveButton->EnableWindow(TRUE);
}

void CJeolDlg::OnLineStart(ULONG64 y)
{
	m_wndPreview.OnLineStart();
	m_resampler.Init();
}


void CJeolDlg::OnLineDrop(ULONG64 y)
{
	m_wndPreview.OnLineDrop();
}


void CJeolDlg::OnLineStop(ULONG64 y)
{
	m_wndPreview.OnLineStop();
}


void CJeolDlg::OnPixel(ULONG64 x, ULONG64 y, const CCachedQueue<CFrame> & queue)
{
	SHORT nSample;
	if (m_resampler.Add(queue, nSample))
	{
// 		BYTE y;
// 		if (nSample > DATA_WHITE_LEVEL)
// 			y = 255;
// 		else if (nSample < DATA_BLACK_LEVEL)
// 			y = 0;
// 		else
// 			y = DATA_NORMALIZE(nSample);


		m_wndPreview.OnPixel(theConfig.NormalizeData(nSample));	
	}

}

void CJeolDlg::OnBnClickedButtonSave()
{
	CString strFileName = CString(_T("scan ")) +
		m_timeFrame.Format("%Y.%m.%d %H-%M-%S") + 
		CString(_T(".png"));

	CImage out;
	out.Create(IMAGE_FULL_W, IMAGE_FULL_H, 24);
	HDC hDC = out.GetDC();
	m_wndPreview.Draw(hDC);
	out.ReleaseDC();

	out.Save(strFileName.GetString(), Gdiplus::ImageFormatPNG);
	
}

void CJeolDlg::OnTimer(UINT_PTR nIDEvent)
{
	m_wndPreview.Invalidate(FALSE);

	CDialog::OnTimer(nIDEvent);
}

void CJeolDlg::OnBnClickedLogButton()
{
	CButton * pButton = (CButton *)GetDlgItem(IDC_LOG_BUTTON);

	if ( pButton->GetCheck() == BST_UNCHECKED )
	{
		theApp.StopLog();
		// pButton->SetCheck(BST_UNCHECKED);
	}
	else
	{
		theApp.StartLog();
		// pButton->SetCheck(BST_CHECKED);
	}
}
